<?php

/**
 * Contains all methods for creating and maneging charectors
 * @author Baagoe
 */
class Character extends XMap {
    public function __construct($configFile = null, $xmlMap = null) {
        parent::__construct($configFile, $xmlMap);
        
        //Loads quest class
        $this->load("quest");
        $this->load("sound");
    }
    
    //Creates NPCs or persons on the map
    public function createCharectors() {
        $charectors = $this->xmlMap->npcs->children();
        if(!empty($charectors)) {
            echo "<div id='charectors'>";
            foreach($charectors as $charector) {
                $this->createCharector($charector);
            }
            echo "<div id='shotSounds'>";
            $this->printCharacterSound();
            echo "</div>";
            echo "</div>";
        }
    }
    
    //Prints the different character sounds
    public function printCharacterSound() {
        $sounds = $this->sound->printSounds("objectSounds", "objectSoundFolder", true);
        if($sounds > 0) {
            foreach($sounds as $sound) {
                echo $sound;
            }
        }
    }
    
    //Creates a charector on the map
    private function createCharector($charObject) {
        if(!empty($charObject)) {
           $name = $charObject->attributes()->name;
           $trigger = $charObject->attributes()->trigger;
           $type = $charObject->attributes()->type;
           $posX = $charObject->attributes()->posX. "px";
           $posY = $charObject->attributes()->posY. "px";
           $hasQuist = false;
           $layer = $charObject->attributes()->layer;
           $immortal = $charObject->attributes()->immortal;
           $dieActionCss = "";
           
           //Is charector immortal
           if($immortal == "true") {
                $immortalClass = "immortal";
           } else {
                $immortalClass = "";
           }
           
           //Getting the charector type information from config file
           if(!empty($type)) {
                if(!empty($this->xmapConfig->characterTypes->$type->attributes()->src)) {
                    $width = $this->xmapConfig->characterTypes->$type->attributes()->width. "px";
                    $height = $this->xmapConfig->characterTypes->$type->attributes()->height. "px";
                    $src = $this->xmapConfig->characterTypes->$type->attributes()->src;
                    $src = $this->folders['skinFolder']. $src;
                    $animation = $this->xmapConfig->characterTypes->$type->attributes()->animation;
                    $shootAnimation = $this->xmapConfig->characterTypes->$type->attributes()->shootAnimation;
                    $amonition = $this->xmapConfig->characterTypes->$type->attributes()->amonition;
                    
                    if($shootAnimation == "true") {
                        $shootAnimationClass = "shootAnimate";
                    } else {
                        $shootAnimationClass = "";
                    }
                } else {
                    $this->error("The given charector type($type) for $name don't exist in the xmapConfig.xml!");
                }
           } else {
            $this->error('No charector type has been given for charector: '. $name);
           }

        
            //Has charector children objects in XML file?
            if($charObject->children()) {
            
                 $appenShootClass = "";
                 
                //There must be quist if trigger is quist!
                if(strstr($trigger, "quest")) {
                    if($charObject->quests) {
                        $hasQuist = true;
                        $quests = $charObject->quests->children();
                        $JSONQuists = $this->quest->createQuest($quests);
                        if(empty($JSONQuists)) {
                            $this->error('You have triggered quests but not given '. $name.  ' any quests!!!');
                        }
                    } else {
                        $hasQuist = false;
                        $this->error('You have triggered quests but not given '. $name.  ' any quests!!!');
                    }
                
                } elseif(strstr($trigger, "shoot")) {
                    $appenShootClass = "shoot";
                    if(isset($amonition)) {
                        $sound = "fxSounds/shotSounds/". $amonition;
                        $this->sound->loadSoundFromObject($sound, $name, false);
                        $amonition = "amo='$amonition'";
                        
                    } else {
                        $amonition = "";
                    }
                } else {
                    $appenShootClass = "";
                }
                
                //Creating trigger for
                if(!empty($trigger)) {
                    $trigger = "trigger $trigger";
                } else { 
                    $trigger = null;
                }
        
                //Creating a path
                if($charObject->path) {
                    $pathPoints = $charObject->path->children();
                    $path = $this->createCharectorPath($pathPoints);
                    $hasPathClass = "hasPath ". $name. "Path";     
                } else {
                    $hasPathClass = "";
                }
                
                //Creating trigger If conditions
                $triggerIf = $charObject->triggerIf;
                
                //Creating trigger if array
                if(!empty($triggerIf)) {
                    $requireNumber = (int)$triggerIf->attributes()->this;
                    $requerement = (string)$triggerIf->attributes()->condition;
                    $isStatement = (string)$triggerIf->attributes()->is;
                    $action = (string)$triggerIf->ifAction;
                    $elseDo = (string)$triggerIf->elseAction;
                    
                    if(empty($elseDo)) {
                        $elseDo = ""; 
                    }
                
                    
                    if($isStatement == "+" || $isStatement == "-" || $isStatement == "="  || $isStatement == "+="  || $isStatement == "-=") {
                        if(is_int($requireNumber) && is_string($requerement) && is_string($action) && is_string($isStatement)) {
                            $JSONTriggerIf = array("requireNumber" => $requireNumber, "requerement" => $requerement, "action" => $action, "triggerObject" => "$name", "elseDo" => $elseDo, "operator" => $isStatement);
                            $JSONTriggerIf = json_encode($JSONTriggerIf);
                        } else {
                            $this->error("triggerIf for $name is not set correctly");
                        }
                    } else {
                        $this->error("triggerIf for $name has not correctly set the 'is' attribute!<br/>It should be: <, >, ==, <=, >=");
                    }
                }
                
                //Creating die action
                if(!empty($charObject->dieAction)) {
                    $dieAction = array("object" => "$name", "action" => $charObject->dieAction);
                    $dieAction = json_encode($dieAction);
                    $dieActionCss = "dieAction";
                    $hasDieAction = true;
                }
            }
            
            //Creating the DIV element for the charector and printing it to screen
            $collisionClass = $this->colObject. " ". $this->stopPlayer;
            $charectorHTML = "<div id='$name' style='position: absolute; background-image: url($src); top: $posY; left: $posX; width: $width; height: $height; z-index: $layer' class='$trigger $hasPathClass NPC $collisionClass $appenShootClass $shootAnimationClass $immortalClass $type $dieActionCss' $amonition></div>";
            //$name = "$('#$name')";
            if(!empty($hasPathClass)) {
              $charectorHTML .= "<script type='text/javascript'>character.loadPath('#$name', $path);</script>";
            }
            
            if(!empty($JSONTriggerIf)) {
                $charectorHTML .= "<script type='text/javascript'>character.loadAction($JSONTriggerIf)</script>";
            }
            
            if(!empty($hasQuist)) {
                $charectorHTML .= "<script type='text/javascript'>character.loadQuests('$name', $JSONQuists);</script>";
            }
            
            if(!empty($hasDieAction)) {
                $charectorHTML .= "<script type='text/javascript'>character.loadDieAction($dieAction)</script>";
            }
            echo $charectorHTML;
        }
        
    }
    
    //Creates a charectors movement path
    private function createCharectorPath($pathPoints) {
        if(!empty($pathPoints)) {
            foreach($pathPoints as $pathPoint) {
                $speed = $pathPoint->attributes()->speed;
                $wait = $pathPoint->attributes()->wait;
                $posX = $pathPoint->attributes()->posX;
                $posY = $pathPoint->attributes()->posY;
                
                $JSONPath[] = array('speed' => $speed, 'wait' => $wait, 'posX' => $posX, 'posY' => $posY);
            }
            $JSONPath = json_encode($JSONPath);
            return $JSONPath;
        } else {
            return null;
        }
    }
}

?>
